<template>
    <div class="">
        <!-- Form -->
        <el-dialog :title="isAdd ? '添加管理员' : '编辑管理员'" :visible.sync="isShow" :before-close="cancel" center>
            <el-form :model="userForm" :rules="rules" ref="userForm">
                <el-form-item label="所属角色" :label-width="formLabelWidth" prop="roleid">
                    <el-select v-model="userForm.roleid" placeholder="请选择">
                        <el-option v-for="item in $store.getters['role/getRoleInfo']" :key="item.id" :value="item.id"
                            :label="item.rolename"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="用户名称" :label-width="formLabelWidth" prop="username">
                    <el-input autocomplete="off" v-model="userForm.username"></el-input>
                </el-form-item>
                <el-form-item label="密码" :label-width="formLabelWidth" prop="password">
                    <el-input autocomplete="off" v-model="userForm.password"></el-input>
                </el-form-item>
                <p>密码为空，则不做修改</p>
                <el-form-item label="状态" :label-width="formLabelWidth" prop="status">
                    <el-switch active-color="#13ce66" inactive-color="#ff4949" v-model="userForm.status"
                        :active-value="1" :inactive-value="2">
                    </el-switch>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="comfirm">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import { userAdd, userEdit, getUserInfo } from '../../request/api'
import { mapActions, mapGetters } from 'vuex'
export default {
    props: ['isShow', 'isAdd'],
    data() {
        return {
            formLabelWidth: '120px',
            userForm: {
                roleid: '',
                username: '',
                password: '',
                status: 1
            },
            rules: {
                username: [
                    { required: true, message: '请输入用户名称', trigger: 'blur' },
                    { min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur' }
                ],
                roleid: [
                    { required: true, message: "请选择上级管理员", trigger: "change" },
                ],
            }
        }
    },
    // computed: {
    //     ...mapGetters({
    //         rolelist: 'role/getRoleInfo'
    //     })
    // },
    mounted() {
        // if (this.rolelist.length == 0) {
        //     this.getRoleAction()
        // }
        if (this.$store.getters["role/getRoleInfo"].length == 0) {
            this.$store.dispatch("role/getRoleAction");
 }
    },

    methods: {
        cancel() {
            this.reset()
            this.$emit('cancel', false)
        },
        ...mapActions({
            // getRoleAction: "role/getRoleAction",
            getCount: "user/getCountAction",
            getUserList: "user/getUserAction",

        }),
        // 封装一个重置方法
        reset() {
            this.userForm = {
                roleid: '',
                username: '',
                password: '',
                status: 1
            };
            // 重置并移除检验结果
            this.$refs.userForm.resetFields()
        },
        // 封装一个获取一条数据事件
        lookup(uid) {
            // console.log(id);
            getUserInfo({ uid }).then(res => {
                console.log(res);
                this.userForm = res.list
                this.userForm.uid = uid
                this.userForm.password = ''
            })
        },
        // 封装一个表单提交事件
        comfirm() {
            // console.log(this.userForm, "表单对象");
            this.$refs.userForm.validate((val) => {
                if (val) {
                    if (this.isAdd) {
                        // 添加流程 
                        userAdd(this.userForm).then((res) => {
                            console.log(res);
                            if (res.code == 200) {
                                this.$message.success(res.msg);
                                // 关闭弹框
                                this.cancel()
                                // 调用列表接口
                                this.getUserList()
                                this.getCount()
                            } else {
                                this.$message.error(res.msg);
                            }
                        })
                    } else {
                        userEdit(this.userForm).then((res) => {
                            if (res.code == 200) {
                                this.$message.success(res.msg);
                                // 关闭弹框
                                this.cancel()
                                // 调用列表接口
                                this.getUserList()
                                this.getCount()
                            } else {
                                this.$message.error(res.msg);
                            }
                        })
                    }

                }
            })

        }
    }
}
</script>
<style scoped lang="scss">

</style>